<template>
  <div>
    <!-- 角色列表 修改角色信息 -->
    <el-dialog
      title="修改角色信息"
      :visible.sync="dialogFormVisible">
      <el-form v-model="selectedRole" style="text-align: left" ref="dataForm">
        <el-form-item label="角色名" label-width="120px" prop="username">
          <el-input v-model="selectedRole.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" label-width="120px" prop="name">
          <el-input v-model="selectedRole.nameZh" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="请求权限" label-width="120px" prop="perms">
          <el-checkbox-group v-model="selectedPermsIds">
            <el-checkbox v-for="(perm,i) in perms" :key="i" :label="perm.id">{{perm.desc_}}</el-checkbox>
          </el-checkbox-group>
        </el-form-item>

        <el-form-item label="菜单权限" label-width="120px" prop="menus">
          <el-tree
            ref="tree"
            :data="menus"
            :props="props"
            show-checkbox
            check-strictly
            :default-checked-keys="selectedMenusIds"
            node-key="id"
            >
          </el-tree>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit(selectedRole)">确 定</el-button>
      </div>
    </el-dialog>
     <!-- 按钮上面的导航 -->
    <!-- <el-row style="margin: 18px 0px 0px 18px "> -->
    <el-row>
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/admin/dashboard' }">管理中心</el-breadcrumb-item>
        <el-breadcrumb-item>系统管理</el-breadcrumb-item>
        <el-breadcrumb-item>角色配置</el-breadcrumb-item>
      </el-breadcrumb>
    </el-row>
    <!-- 搜索与添加按钮 -->
    <div style="margin-top: 15px; margin-left: 12px;">
      <el-input  placeholder="请输入角色描述" v-model="queryKey2" style="width: 530px;float:left;"  clearable>

         <el-select v-model="queryKey1" style="width: 130px;" slot="prepend" placeholder="请选择状态">
           <el-option label="" value=""></el-option>
          <el-option label="启用" value="1"></el-option>
          <el-option label="禁用" value="0"></el-option>
        </el-select>
        <!-- <el-button slot="append" icon="el-icon-search" @click="findByQueryKey()"></el-button> -->
      </el-input>
       <el-button style="float:left;" type="primary" @click="findByQueryKey()" icon="el-icon-search">搜索</el-button>
      <div style="float:left;"><role-create @onSubmit="listRoles()"></role-create></div>
    </div>

    <el-card style="margin: 15px 1%;width: 98%">
      <el-table
        :data="roles"
        border
        style="width: 100%"
        :max-height="tableHeight">
        <el-table-column
          prop="id"
          label="id"
          width="100">
        </el-table-column>
        <el-table-column
          prop="name"
          label="角色名"
          fit>
        </el-table-column>
        <el-table-column
          prop="nameZh"
          label="角色描述"
          fit>
        </el-table-column>
        <el-table-column
          label="启用"
          width="110">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.enabled"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="(value) => commitStatusChange(value, scope.row)">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          width="120">
          <template slot-scope="scope">
             <el-button
              @click="editRole(scope.row)"
              type="primary"
              size="small"
              icon="el-icon-edit" circle>
            </el-button>
            <el-button
            @click="deleteRole(scope.row.id)"
              type="danger"
              size="small"
              icon="el-icon-delete" circle>
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
  import RoleCreate from './RoleCreate'
  export default {
    name: 'UserRole',
    components: {RoleCreate},
    data () {
      return {
        dialogFormVisible: false,
        roles: [],
        perms: [],
        menus: [],
        selectedRole: [],
        selectedPermsIds: [],
        selectedMenusIds: [],
        queryKey1: '',
        queryKey2: '',
        props: {
          id: 'id',
          label: 'nameZh',
          children: 'children'
        }
      }
    },
    mounted () {
      this.listRoles()
      this.listPerms()
      this.listMenus()
    },
    computed: {
      tableHeight () {
        return window.innerHeight - 240
      }
    },
    methods: {
      listRoles () {
        var _this = this
        this.$axios.get('/admin/role').then(resp => {
          if (resp && resp.status === 200) {
            _this.roles = resp.data.result
          }
        })
      },
      listPerms () {
        var _this = this
        this.$axios.get('/admin/role/perm').then(resp => {
          if (resp && resp.data.code === 200) {
            _this.perms = resp.data.result
          }
        })
      },
      listMenus () {
        var _this = this
        this.$axios.get('/admin/role/menuAll').then(resp => {
          if (resp && resp.data.code === 200) {
            _this.menus = resp.data.result
          }
        })
      },
      commitStatusChange (value, role) {
        if (role.id !== 1) {
          this.$confirm('是否更改角色状态？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            this.$axios.put('/admin/role/status', {
              enabled: value,
              id: role.id
            }).then(resp => {
              if (resp && resp.data.code === 200) {
                if (value) {
                  this.$message('角色 [' + role.nameZh + '] 已启用')
                } else {
                  this.$message('角色 [' + role.nameZh + '] 已禁用')
                }
              }
              this.listRoles()
            })
          }).catch(() => {
            role.enabled = !role.enabled
            this.$message({
              type: 'info',
              message: '已取消'
            })
          })
        } else {
          role.enabled = true
          this.$alert('无法禁用系统管理员！')
        }
      },
    editRole (role) {
      this.dialogFormVisible = true
      this.selectedRole = role
      let permIds = []
      for (let i = 0; i < role.perms.length; i++) {
        permIds.push(role.perms[i].id)
      }
      this.selectedPermsIds = permIds
      let menuIds = []
      console.log(role.menus)
      for (let i = 0; i < role.menus.length; i++) {
        menuIds.push(role.menus[i].id)
        for (let j = 0; j < role.menus[i].children.length; j++) {
          menuIds.push(role.menus[i].children[j].id)
        }
      }
      this.selectedMenusIds = menuIds
      // 判断树是否已经加载，第一次打开对话框前树不存在，会报错。所以需要设置 default-checked
      if (this.$refs.tree) {
        this.$refs.tree.setCheckedKeys(menuIds)
      }
      // this.listRoles()
    },
    onSubmit (role) { // 确定修改角色role后向后端发送
        let _this = this
        // 根据视图绑定的角色 id 向后端传送角色信息
        let perms = []
        for (let i = 0; i < _this.selectedPermsIds.length; i++) {
          for (let j = 0; j < _this.perms.length; j++) {
            if (_this.selectedPermsIds[i] === _this.perms[j].id) {
              perms.push(_this.perms[j])
            }
          }
        }
        this.$axios.put('/admin/role', {
          id: role.id,
          name: role.name,
          nameZh: role.nameZh,
          enabled: role.enabled,
          perms: perms
        }).then(resp => {
          if (resp && resp.data.code === 200) {
           // 从后端返回信息
              this.$alert(resp.data.message, '提示', {
                type: 'success',
                dangerouslyUseHTMLString: true
              })
            this.dialogFormVisible = false
            this.listRoles()
          } else {
              // 从后端返回错误信息
              this.$alert(resp.data.message, '错误提示', {
                type: 'error',
                dangerouslyUseHTMLString: true
              })
          }
        })

        this.$axios.put('/admin/role/menu?rid=' + role.id, {
         // menusIds: this.$refs.tree.getCheckedKeys() // 获取子节点
         menusIds: this.$refs.tree.getCheckedKeys().concat(this.$refs.tree.getHalfCheckedKeys()) // 这个可以父和子节点id都i获取
        }).then(resp => {
          if (resp && resp.data.code === 200) {
          }
        })
      },

     deleteRole (id) {
        this.$confirm('此操作将永久删除, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
            this.$axios
              .post('/admin/role/del?id=' + id).then(resp => {
              if (resp && resp.data.code === 200) {
                // 从后端返回信息
                this.$alert(resp.data.message, '提示', {
                  type: 'success',
                  dangerouslyUseHTMLString: true
                })
              }
              this.listRoles()
            })
          }
        ).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      },
      findByQueryKey () { // 根据关键字查询
          var _this = this
          this.$axios.get('/admin/role/findByQueryKey?queryKey1=' + this.queryKey1 + '&queryKey2=' + this.queryKey2).then(resp => {
            if (resp && resp.data.code === 200) {
                _this.roles = resp.data.result
                // 从后端返回信息
              this.$alert(resp.data.message, '提示', {
                type: 'success',
                dangerouslyUseHTMLString: true
              })
            }
          })
        }
      }
  }
</script>

<style scoped>
  .add-button {
    float: left;
    margin: 18px 0 18px 10px;
  }
</style>
